import 'package:badges/badges.dart' as myBadges;
import 'package:flutter/material.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/models/common/LevelOption.dart';
import 'package:ichinesereader/pages/common/levelStandards.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';

class LevelChioceChipWidget extends StatefulWidget {
  LevelChioceChipWidget({Key? key}) : super(key: key);

  @override
  State<LevelChioceChipWidget> createState() => _LevelChioceChipWidgetState();
}

class _LevelChioceChipWidgetState extends State<LevelChioceChipWidget> {
  var box = Hive.box('appconfig');

  // var levelOptions = Constants.levelOptions;

  @override
  Widget build(BuildContext context) {
    Log.debug('build LevelChioceChipWidget');
    // bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
    //     Constants.NanhaiRole_Teacher;
    // //if (isTeacher) {
    // levelOptions.removeWhere((element) {
    //   var item = element as LevelOption;
    //   return item.value.toString() == 'pre';
    // });
    // //}
    final BooksController _booksController = Get.find();
    return myBadges.Badge(
      //shape: BadgeShape.square,
      //elevation: 3,
      badgeStyle: myBadges.BadgeStyle(
        badgeColor: Colors.red.withOpacity(0.8),
      ),
      position: myBadges.BadgePosition.topEnd(top: 0, end: 3),
      badgeContent: GestureDetector(
        onTap: () {
          Get.to(() => LevelStandardsPage(isEvaluation: false));
        },
        child: Tooltip(
          message: 'Level Comparison Chart'.tr,
          child: Icon(
              const IconData(
                0xf04e5,
                fontFamily: 'MaterialIcons',
              ),
              color: Colors.white,
              size: 30),
        ),
      ),
      child: Container(
        height: 45.0,
        child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: Constants.levelOptions.length,
          itemBuilder: ((context, index) {
            return Padding(
              padding: const EdgeInsets.symmetric(
                horizontal: 3.0,
              ),
              child: FilterChip(
                padding: EdgeInsets.symmetric(horizontal: 12.0),
                shape: BeveledRectangleBorder(
                  side: BorderSide(color: Colors.white, width: 0.3),
                  borderRadius: BorderRadius.circular(1.0),
                ),
                backgroundColor: hexStringToHexInt(
                  Constants.levelOptions[index].color.toString(),
                ),
                label: Text(
                  Constants.levelOptions[index].label
                      .toString()
                      .replaceAll('Level 0', 'N/A')
                      .replaceAll('Level ', ''),
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.white),
                ),
                //onSelected: (bool value) {},
                onSelected: (bool value) {
                  if (value == false) return;

                  if (Constants.levelOptions[index].value.toString() == 'pre') {
                    Get.toNamed('/student/basicCardList');
                  }

// goto word  card page once find pre
                  // value.forEach((element) {
                  //   var item = element as LevelOption;
                  //   if (item.value.toString() == 'pre') {
                  //     Get.toNamed('/student/basicCardList');
                  //   }
                  // });
                  // value.removeWhere((element) {
                  //   var item = element as LevelOption;
                  //   return item.value.toString() == 'pre';
                  // });
                  BookSearchDto dto = new BookSearchDto();
                  dto.proficiencyLevel = [];
                  List<String> levelstr = [];
                  List<LevelOption> levelOptions = [];

                  var item = Constants.levelOptions[index];
                  levelstr.add(item.value.toString());
                  levelOptions.add(item);

                  _booksController.setBookSearchDto(dto);
                  _booksController.setLevelOptions(levelOptions);
                  if (levelstr.length > 0) {
                    dto.proficiencyLevel?.addAll(levelstr);
                    _booksController.fetchSearchOpenLibraryBookItems(
                        Constants.pageLimit, 0, dto);
                  } else {
                    _booksController.fetchOpenReadingItems(
                        Constants.pageLimit, 0);
                  }
                },
              ),
            );
          }),
        ),
      ),
    );
  }
}
